package com.zd.lib.utils;

import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.TextUtils;


/**
 * apk工具类
 * 
 * @author xiaoyu.ding
 *
 */
public class ApkUtils {

	private static final String APPVERSION = "appversion";

	/**
	 * 获取包名
	 * 
	 * @param context
	 * @return
	 */
	public static String getPackageName(Context context) {
		if (context != null) {
			return context.getPackageName();
		}
		return "";
	}

	/**
	 * 获取版本号
	 * 
	 * @param context
	 * @return
	 */
	public static int getVersionCode(Context context) {
		try {
			PackageManager pm = context.getPackageManager();
			PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
			return pi.versionCode;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}
	
	//校验是否是版本升级
	public static boolean isAppVersionUpdate(Context context) {
		int currentAppVersion = ApkUtils.getVersionCode(context);
		int saveAppVersionCode = SPUtils.getIntFromSP(context,APPVERSION, -1);
		if(saveAppVersionCode < currentAppVersion) {
			return true;
		}
		return false;
	}

	/**
	 * 获取版本名称
	 * 
	 * @param context
	 * @return
	 */
	public static String getVersionName(Context context) {
		String versionName = "";
		try {
			PackageManager pm = context.getPackageManager();
			PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
			versionName = pi.versionName;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return versionName;
	}

	/**
	 * 判断APK是否处于Debug模式
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isDebugMode(Context context) {
		if (context == null) {
			return false;
		}
		ApplicationInfo info = context.getApplicationInfo();
		return (0 != ((info.flags) & ApplicationInfo.FLAG_DEBUGGABLE));
	}
	
    
    /* 获取application中指定的meta-data
    * @return 如果没有获取成功(没有对应值，或者异常)，则返回值为空
    */
   private static String getAppMetaData(Context ctx, String key) {
       if (ctx == null || TextUtils.isEmpty(key)) {
           return null;
       }
       String resultData = null;
       try {
           PackageManager packageManager = ctx.getPackageManager();
           if (packageManager != null) {
               ApplicationInfo applicationInfo = packageManager.getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);
               if (applicationInfo != null) {
                   if (applicationInfo.metaData != null) {
                       resultData = applicationInfo.metaData.getString(key);
                   }
               }
           }
       } catch (PackageManager.NameNotFoundException e) {
           e.printStackTrace();
       }

       return resultData;
   }
   /**
    * 设置百度channel--BaiduMobAd_CHANNEL
    * @param ctx
    * @return
    */
//   public static String setBaiduMobAdChannel(Context ctx) {
//	   if (ctx == null ) {
//		   return null;
//	   }
//	   String resultData = null;
//	   try {
//		   PackageManager packageManager = ctx.getPackageManager();
//		   if (packageManager != null) {
//			   ApplicationInfo applicationInfo = packageManager.getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);
//			   if (applicationInfo != null) {
//				   if (applicationInfo.metaData != null) {
////					   resultData = applicationInfo.metaData.getString(key);
////					   Toast.makeText(ctx, "设置百度channel-begin--"+getChannel(ctx), 0).show();
//					   StatService.setAppChannel(ctx,getChannel(ctx),true);
//				   }
//			   }
//		   }
//	   } catch (PackageManager.NameNotFoundException e) {
//		   e.printStackTrace();
//	   }
//
//	   return resultData;
//   }
//   public static void setUmengChannel(Context ctx){
//	   AnalyticsConfig.setChannel(getBaiduChannel(ctx));
//   }
//
   /**
    * 获取channel Name
    * @param context
    * @return
    */
   public static String getChannel(Context context) {
	   return getAppMetaData(context, "UMENG_CHANNEL");
   }
   public static String getBaiduChannel(Context context) {
	   return getAppMetaData(context, "BaiduMobAd_CHANNEL");
   }
   /**
    * 判断应用是否正在运行
    * 
    * @param context
    * @return
    */ 
   public static boolean isRunning(Context context) { 
       ActivityManager am = (ActivityManager) context 
               .getSystemService(Context.ACTIVITY_SERVICE); 
       List<RunningAppProcessInfo> list = am.getRunningAppProcesses(); 
       for (RunningAppProcessInfo appProcess : list) { 
           String processName = appProcess.processName; 
           if (processName != null && processName.equals(context.getPackageName())) { 
               return true; 
           } 
       } 
       return false; 
   }
   /**
    * 判断是否在后台运行
    * @param context
    * @return true 为后台，false为前台
    */
   public static boolean isBackground(Context context) {

	    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
	    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
	    for (RunningAppProcessInfo appProcess : appProcesses) {
	         if (appProcess.processName.equals(context.getPackageName())) {
	                if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
	                          return true;
	                }else{
	                          return false;
	                }
	           }
	    }
	    return false;
	}

	/**
	 * 打开相册
	 * @param context
	 * @param resultcode
     */
	public  static  void openLocalPic(Context context,int resultcode) {
		if (context instanceof Activity) {
			Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
			// intent.setType("image/*");
			intent.setAction("android.intent.action.PICK");
			intent.setType("vnd.android.cursor.dir/image");
			((Activity)context).startActivityForResult(intent, resultcode);
		}

	}
	/**
	 * @param @param data
	 * @return void
	 * @Description: 获取从相册选择的图片且截图
	 */
	public  static String getSelectedPic(Context context,Intent data) {
		try {

			Uri selectedImage = data.getData();
			String[] filePathColumn = {MediaStore.Images.Media.DATA};
			Cursor cursor = context.getContentResolver().query(selectedImage,
					filePathColumn, null, null, null);
			cursor.moveToFirst();
			int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
			String picturePath = cursor.getString(columnIndex);
			cursor.close();
			return "file:///" + picturePath;
		} catch (Exception e) {
			return null;
		}
	}
}
